//
//  LXSongsViewController.m
//  bgms_d
//
//  Created by lachesis-zhu on 2019/1/18.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXSongsViewController.h"
#import "ZYPlayingViewController.h"
#import "GLMusicListViewController.h"
#import "GLMiniMusicView.h"
#import "ZYMusicTool.h"
#import "ZYMusic.h"
#import "ZYAudioManager.h"

@interface LXSongsViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) NSArray *storyMusicArray;

@end

@implementation LXSongsViewController

#pragma mark -  public method
- (void)musicScrollTo
{
    NSArray *array = [self.tableView visibleCells];
    if (array.count == 0) {
        return;
    }
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITableViewCell *cell = obj;
        cell.contentView.backgroundColor = kAppThemeColor;
    }];
    
    MPMediaItem *item = [ZYAudioManager defaultManager].mpcVC.nowPlayingItem;
    __block NSUInteger index = 0;
    [self.storyMusicArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ZYMusic *music = obj;
        if (music.item == item) {
            index = idx;
        }
    }];
    
    self.currentIndex = index;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    [self performSelector:@selector(delayMethod:) withObject:indexPath afterDelay:1];
}

- (void)delayMethod:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = kAppSelectedColor;
}
#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];
    
    [self loadUI];
    [self buildNavi];
    
    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark -  over write

#pragma mark -  setter & getter
- (void)setMusicListArray:(NSMutableArray *)musicListArray {
    NSArray *array = [musicListArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        ZYMusic *music1 = obj1;
        ZYMusic *music2 = obj2;
        return [music1.name compare:music2.name options:NSNumericSearch];
    }];
    _musicListArray = [[NSMutableArray alloc] initWithArray:array];
    if (!self.storyMusicArray) {
        self.storyMusicArray = [[NSArray alloc] initWithArray:_musicListArray];
    }
    [self.tableView reloadData];
}

- (void)setSearchString:(NSString *)searchString {
    _searchString = searchString;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
    [self.storyMusicArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ZYMusic *music = obj;
        if ([music.name rangeOfString:searchString].location != NSNotFound) {
            [searchArray addObject:music];
        }
    }];
    self.musicListArray = [[NSMutableArray alloc] initWithArray:searchArray];
}

- (void)setCancelSearch:(BOOL)cancelSearch
{
    _cancelSearch = cancelSearch;
    self.musicListArray = [[NSMutableArray alloc] initWithArray:self.storyMusicArray];
    [self.tableView reloadData];
}
#pragma mark -  private method
- (void)initializationData
{
    self.currentIndex = -1;
}

- (void)loadUI
{
    self.view.backgroundColor = kAppLightBackgroundColor;
    self.tableView.separatorColor = UICOLOR_FROM_RGB(45, 185, 105);
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"musiclist"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{
    
}

#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle


#pragma mark -  delegate
#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LXAutoLength lx_L:75];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.musicListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musiclist" forIndexPath:indexPath];
    UILabel *text = [cell.contentView viewWithTag:1];
    UILabel *name = [cell.contentView viewWithTag:2];;
    if (!text) {
        text = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, kScreenWidth, [LXAutoLength lx_L:30])];
        text.tag = 1;
        text.font = [UIFont systemFontOfSize:15];
        text.textColor = kAppNormalTextColor;
        text.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:text];
        [cell.contentView drawAroundViewWithLocation:DrawBottom color:kAppGrayTextColor insets:UIEdgeInsetsMake(0, 0, 0.5, 0)];
        name = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 20)];
        name.font = [UIFont systemFontOfSize:12];
        name.textColor = kAppNormalTextColor;
        name.tag = 2;
        name.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:name];
    }
    ZYMusic *music = self.musicListArray[indexPath.row];
    text.text = music.name;
    name.text = music.singer;
    MPMediaItem *lastItem = [ZYAudioManager defaultManager].nowPlayItem;
    if (lastItem && [lastItem isEqual:music.item]) {
        cell.contentView.backgroundColor = kAppSelectedColor;
    } else {
        cell.contentView.backgroundColor = kAppThemeColor;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ZYMusic *music = self.musicListArray[indexPath.row];
    [(GLMusicListViewController *)self.containerController selectPlayMusic:music withAlbum:nil];
    self.currentIndex = (int)indexPath.row;
    self.cancelSearch = YES;
    self.musicListArray = [[NSMutableArray alloc] initWithArray:self.storyMusicArray];
}

@end
